/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.order;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.order.HkShopUserOrderAfter;
import com.thinkgem.jeesite.modules.hk.shop.dao.order.HkShopUserOrderAfterDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.order.HkShopUserOrderAfterImages;
import com.thinkgem.jeesite.modules.hk.shop.dao.order.HkShopUserOrderAfterImagesDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.order.HkShopUserOrderAfterLog;
import com.thinkgem.jeesite.modules.hk.shop.dao.order.HkShopUserOrderAfterLogDao;

/**
 * 订单售后Service
 *
 * @author 罗天文
 * @version 2018-08-22
 */
@Service
@Transactional(readOnly = true)
public class HkShopUserOrderAfterService extends CrudService<HkShopUserOrderAfterDao, HkShopUserOrderAfter> {

    @Autowired
    private HkShopUserOrderAfterImagesDao hkShopUserOrderAfterImagesDao;
    @Autowired
    private HkShopUserOrderAfterLogDao hkShopUserOrderAfterLogDao;

    public HkShopUserOrderAfter get(String id) {
        HkShopUserOrderAfter hkShopUserOrderAfter = super.get(id);
        hkShopUserOrderAfter.setHkShopUserOrderAfterImagesList(hkShopUserOrderAfterImagesDao.findList(new HkShopUserOrderAfterImages(hkShopUserOrderAfter)));
        hkShopUserOrderAfter.setHkShopUserOrderAfterLogList(hkShopUserOrderAfterLogDao.findList(new HkShopUserOrderAfterLog(hkShopUserOrderAfter)));
        return hkShopUserOrderAfter;
    }

    public List<HkShopUserOrderAfter> findList(HkShopUserOrderAfter hkShopUserOrderAfter) {
        return super.findList(hkShopUserOrderAfter);
    }

    public Page<HkShopUserOrderAfter> findPage(Page<HkShopUserOrderAfter> page, HkShopUserOrderAfter hkShopUserOrderAfter) {
        return super.findPage(page, hkShopUserOrderAfter);
    }

    @Transactional(readOnly = false)
    public void save(HkShopUserOrderAfter hkShopUserOrderAfter) {
        super.save(hkShopUserOrderAfter);
        for (HkShopUserOrderAfterImages hkShopUserOrderAfterImages : hkShopUserOrderAfter.getHkShopUserOrderAfterImagesList()) {
            if (hkShopUserOrderAfterImages.getId() == null) {
                continue;
            }
            if (HkShopUserOrderAfterImages.DEL_FLAG_NORMAL.equals(hkShopUserOrderAfterImages.getDelFlag())) {
                if (StringUtils.isBlank(hkShopUserOrderAfterImages.getId())) {
                    hkShopUserOrderAfterImages.setAfter(hkShopUserOrderAfter);
                    hkShopUserOrderAfterImages.preInsert();
                    hkShopUserOrderAfterImagesDao.insert(hkShopUserOrderAfterImages);
                } else {
                    hkShopUserOrderAfterImages.preUpdate();
                    hkShopUserOrderAfterImagesDao.update(hkShopUserOrderAfterImages);
                }
            } else {
                hkShopUserOrderAfterImagesDao.delete(hkShopUserOrderAfterImages);
            }
        }
        for (HkShopUserOrderAfterLog hkShopUserOrderAfterLog : hkShopUserOrderAfter.getHkShopUserOrderAfterLogList()) {
            if (hkShopUserOrderAfterLog.getId() == null) {
                continue;
            }
            if (HkShopUserOrderAfterLog.DEL_FLAG_NORMAL.equals(hkShopUserOrderAfterLog.getDelFlag())) {
                if (StringUtils.isBlank(hkShopUserOrderAfterLog.getId())) {
                    hkShopUserOrderAfterLog.setAfter(hkShopUserOrderAfter);
                    hkShopUserOrderAfterLog.preInsert();
                    hkShopUserOrderAfterLogDao.insert(hkShopUserOrderAfterLog);
                } else {
                    hkShopUserOrderAfterLog.preUpdate();
                    hkShopUserOrderAfterLogDao.update(hkShopUserOrderAfterLog);
                }
            } else {
                hkShopUserOrderAfterLogDao.delete(hkShopUserOrderAfterLog);
            }
        }
    }

    @Transactional(readOnly = false)
    public void delete(HkShopUserOrderAfter hkShopUserOrderAfter) {
        super.delete(hkShopUserOrderAfter);
        hkShopUserOrderAfterImagesDao.delete(new HkShopUserOrderAfterImages(hkShopUserOrderAfter));
        hkShopUserOrderAfterLogDao.delete(new HkShopUserOrderAfterLog(hkShopUserOrderAfter));
    }


    @Transactional(readOnly = false)
    public void agreeReimburse(HkShopUserOrderAfter hkShopUserOrderAfter) {
        dao.agreeReimburse(hkShopUserOrderAfter);
    }

    @Transactional(readOnly = false)
    public void refuseReimburse(HkShopUserOrderAfter hkShopUserOrderAfter) {
        dao.refuseReimburse(hkShopUserOrderAfter);
    }


    @Transactional(readOnly = false)
    public void agreeReturnOfGoods(HkShopUserOrderAfter hkShopUserOrderAfter) {
        dao.agreeReturnOfGoods(hkShopUserOrderAfter);
    }

    @Transactional(readOnly = false)
    public void refuseReturnOfGoods(HkShopUserOrderAfter hkShopUserOrderAfter) {
        dao.refuseReturnOfGoods(hkShopUserOrderAfter);
    }


    @Transactional(readOnly = false)
    public void logisticsMsg(String orderNum, String ids, String receiptName, String receiptPhone, String provinceId, String cityId, String areaId, String receipAddress) {
        HkShopUserOrderAfter hkShopUserOrderAfter = new HkShopUserOrderAfter();
        dao.logisticsMsg(orderNum, ids, receiptName, receiptPhone, provinceId, cityId, areaId, receipAddress);
        hkShopUserOrderAfter.setId(ids);
        dao.agreeReturnOfGoods(hkShopUserOrderAfter);
    }


}